<%--
  Created by IntelliJ IDEA.
  User: 29688
  Date: 2021/5/9
  Time: 16:26
  To change this template use File | Settings | File Templates.
--%>
<%--
  Created by IntelliJ IDEA.
  User: 29688
  Date: 2021/3/28
  Time: 13:07
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <meta charset="utf-8">
    <title>书籍审核</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" href="../../../layui-v2.5.7/layui/css/layui.css" media="all">
    <script src="../../../layui-v2.5.7/layui/layui.js" charset="utf-8"></script>
    <script src="../../../js/jquery.js"></script>
</head>
<body>

<table class="layui-hide" id="test" lay-filter="test">
</table>

<script type="text/html" id="barDemo">
    <a class="layui-btn layui-btn-xs" lay-event="check">书籍审核</a>

</script>
<script type="text/html" id="toolbarDemo">
    <div>
        <button type="button" class="layui-btn layui-btn-sm" lay-event="checkAll">
            <i class="layui-icon">&#xe642;</i> 批量审核
        </button>
        <button class="layui-btn layui-btn-sm" lay-event="getCheckData">获取选中行数据</button>
    </div>
</script>
<script>
    layui.use(['table','layer'], function () {
        var table = layui.table;
        var layer=layui.layer;
        table.render({
            elem: '#test'
            , url: '/book/selectToCheck',
            toolbar: '#toolbarDemo' //开启头部工具栏，并为其绑定左侧模板
            , title: '审核书籍数据表'
            , cols: [[
                /*设置序号*/
                {type: 'numbers', fixed: 'left', title: "id"},
                {type: 'checkbox', fixed: 'left'}
                , {field: 'bid', title: 'ID', width: 80, fixed: 'left', hide: true}
                , {field: 'bname', title: '书名', width: 160, edit: 'text', sort: true}
                , {
                    field: 'price', title: '单价', width: 150, text: '未绑定', edit: 'text',sort: true
                }
                , {field: 'description', title: '描述', width: 200, edit: 'text'}
                , {field: 'count', title: '库存', width: 100,sort: true}
                , {fixed: 'right', title: '操作', toolbar: '#barDemo', width: 150}
            ]]
            , page: true,
            skin: 'row',
            even: 'true',
            id: 'bookTable'
        });

        //头工具栏事件
        table.on('toolbar(test)', function (obj) {
            var checkStatus = table.checkStatus(obj.config.id);
            switch (obj.event) {
                case 'getCheckData':
                    var data = checkStatus.data;
                    layer.alert(JSON.stringify(data));
                    break;
                case 'checkAll':
                    layer.confirm('真的审核所有选中行？', {icon: 3, title: '询问'}, function () {
                        var data = checkStatus.data;
                        if (data.length > 0) {
                            /*后端删除*/
                            var ids = []
                            for (let i = 0; i < data.length; i++) {
                                ids.push(data[i].bid)
                            }
                            console.log(ids);
                            /*前端删除*/
                            layer.confirm('确定审核通过的选中' + data.length + '条数据？', {icon: 3, title: '提示信息'}, function (index) {
                                //layui中找到CheckBox所在的行，并遍历找到行的顺序
                                $("div.layui-table-body table tbody input[name='layTableCheckbox']:checked").each(function () { // 遍历选中的checkbox
                                    n = $(this).parents("tbody tr").index();  // 获取checkbox所在行的顺序
                                    //移除行
                                    $("div.layui-table-body table tbody ").find("tr:eq(" + n + ")").remove();
                                    //如果是全选移除，就将全选CheckBox还原为未选中状态
                                    $("div.layui-table-header table thead div.layui-unselect.layui-form-checkbox").removeClass("layui-form-checked");
                                });
                                $.ajax({
                                    type: "post",
                                    data: {"ids": ids},
                                    url: "/book/checkByIds",
                                    dataType: 'json',
                                    traditional: true,
                                    success:function (data){
                                        if(data>0){
                                            layer.msg("审核成功");
                                        }
                                    }
                                })
                                layer.close(index);
                            })
                        } else {
                            layer.msg("请选择需要审核的行");
                        }
                    });
                    break;

            }

        });




        //监听行工具事件
        table.on('tool(test)', function (obj) {
            var data = obj.data;
            if (obj.event === 'check') {
                layer.confirm('真的审核通过行么', function (index) {
                    obj.del();
                    layer.close(index);
                    $.ajax({
                        type:'get',
                        url:"/book/bookCheckById?bid="+obj.data.bid,
                        success:function(data) {
                            if(data>0){
                                layer.msg("审核成功！")
                            }
                        }
                    })
                });
            }
        });

    });

</script>

</body>
</html>

